package org.example.juc.sync.v11;

import java.util.concurrent.TimeUnit;

/**
 * Copyright (C), 2022, szw
 * FileName: SynchronizedTestV11
 *
 * @author songyafei
 * Date:     2022/1/25 9:47
 * Description: 版本11
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
public class SynchronizedTestV11 {
    int count = 0;
    synchronized void m() {
        System.out.println(Thread.currentThread().getName() + " start");
        while(true) {
            count ++;
            System.out.println(Thread.currentThread().getName() + " count = " + count);
            try {
                TimeUnit.SECONDS.sleep(1);

            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            if(count == 5) {
                //此处抛出异常，锁将被释放，要想不被释放，可以在这里进行catch，然后让循环继续
                int i = 1/0;
                System.out.println(i);
            }
        }
    }

    public static void main(String[] args) {
        SynchronizedTestV11 t = new SynchronizedTestV11();
        Runnable r = new Runnable() {

            @Override
            public void run() {
                t.m();
            }

        };
        new Thread(r, "t1").start();

        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        new Thread(r, "t2").start();
    }
}
